package com.lagou.mall.esflow.server.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @Author xiazhenyu
 * @Description
 * @Date 3:57 下午 2021/2/4
 **/
@SuppressWarnings("rawtypes")
public class JsonUtil {


    public static String toString(Object object) {
        return JSON.toJSONString(object);
    }


    public static <T> T parseFromJsonStr(String jsonStr, Class<T> clazz) {
        return JSON.parseObject(jsonStr, clazz);
    }


    public static <T> List<T> parseListFromJsonStr(String jsonStr, Class<T> clazz) {
        return JSON.parseArray(jsonStr, clazz);
    }


    public static <B> Map<String, B> parseToMap(String jsonString, Class<B> clazzB) {
        Map jsonParseMap = JSONObject.parseObject(jsonString, new TypeReference<Map<String, B>>() {
        }.getType());
        Map<String, B> result = new HashMap<>();
        for (Object object : jsonParseMap.entrySet()) {
            Entry entry = (Entry) object;
            result.putIfAbsent((String) entry.getKey(), ((JSONObject) entry.getValue()).toJavaObject(clazzB));
        }
        return result;
    }


    public static Type getType() {

        Type supperClass = JsonUtil.class.getGenericSuperclass();
        return ((ParameterizedType) supperClass).getActualTypeArguments()[0];

    }


}
